package webServlets;

import domain.Account;
import service.AccountService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class SignOnFormServlet extends HttpServlet {
    private static final String SIGN_ON = "/WEB-INF/catalog/Main.jsp";
    private static final String ERROR = "/WEB-INF/common/Error.jsp";
    private Account account;
    private AccountService accountService;
    private String username = "";
    private String password;
    private String valistr;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        username = request.getParameter("username");
        password = request.getParameter("password");
        valistr = request.getParameter("valistr");

        accountService = new AccountService();
        account = accountService.getAccount(username,password);

        //验证码不为空
        if (!valistr.equals("")) {
            //验证码验证
            String text = ValiImageServlet.text;
            String text1 = text.toUpperCase();//全部为大写
            String text2 = text.toLowerCase();//全部为小写
            if (valistr.equals(text1) || valistr.equals(text2) || valistr.equals(text)) {
                if (account != null) {
                    HttpSession session = request.getSession();
                    session.setAttribute("account",account);
                    publicValue.username = username;
                    request.getRequestDispatcher(SIGN_ON).forward(request, response);
                } else
                    request.getRequestDispatcher(ERROR).forward(request, response);
            } else {
                request.getRequestDispatcher(ERROR).forward(request, response);
            }
        } else {
            request.getRequestDispatcher(ERROR).forward(request, response);
        }
    }
}
